/**   
 *    
 * 项目名称：SmartKungFuService   
 * 类名称：GymDetailServlet   
 * 类描述：   场馆详情接口
 * 创建人：zhangl  
 * 创建时间：2016年9月26日 下午4:33:11   
 * 修改人：zhangl   
 * 修改时间：2016年9月26日 下午4:33:11   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.dao.MstCodeMapper;
import com.lst.entity.request.GymDetailReqPara;
import com.lst.entity.response.GymDetailResPara;
import com.lst.model.ClientNum;
import com.lst.model.GymnasiumDetail;
import com.lst.model.MstCode;
import com.lst.model.MstGymnasium;
import com.lst.service.ClientNumService;
import com.lst.service.GymnasiumDetailService;
import com.lst.service.MstGymnasiumService;
import com.lst.utils.CommFunUtils;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: GymDetailServlet
 * @Description:场馆详情接口
 * @author zhangl
 * @date 2016年9月26日 下午4:33:11
 *
 */
public class GymDetailServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private MstGymnasiumService mstGymnasiumService = ctx.getBean(MstGymnasiumService.class); 

	private GymnasiumDetailService gymnasiumDetailService = ctx.getBean(GymnasiumDetailService.class);

	private ClientNumService clientNumService = ctx.getBean(ClientNumService.class);

	private MstCodeMapper mstCodeMapper = ctx.getBean(MstCodeMapper.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);

		GymDetailResPara resPara = new GymDetailResPara();

		try {
			GymDetailReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				Integer gymid = Integer.valueOf(reqPara.getGymid());									
				Integer unicourse = Integer.valueOf(reqPara.getUnicourse());
				Integer elsecourse = Integer.valueOf(reqPara.getElsecourse());
				String userid = reqPara.getUserid();//用户ID				

				PageBounds pageBounds = new PageBounds();

				//场馆
				MstGymnasium gym = mstGymnasiumService.selectByPrimaryKey(gymid);

				MstCode mc = mstCodeMapper.selectByPrimaryKey(gym.getGymtype());
				gym.setGymtypename(mc.getName());

				Integer favoritenum = CommFunUtils.opNum(null, gym.getId(), CommCode.SYSTEM_CARESEL_RANGE_GYM, CommCode.SYSTEM_COUNT_FAVORITE, clientNumService);
				//分享量
				Integer sharenum = CommFunUtils.opNum(null, gym.getId(), CommCode.SYSTEM_CARESEL_RANGE_GYM, CommCode.SYSTEM_COUNT_SHARE, clientNumService);
				//浏览量
				Map<String, Object> browsemap = new HashMap<String,Object>();

				browsemap.put("enabled", true);
				browsemap.put("isDeleted", false);	
				browsemap.put("numtype", CommCode.SYSTEM_COUNT_BROWSE);
				browsemap.put("actid", gymid);
				browsemap.put("acttype", CommCode.SYSTEM_CARESEL_RANGE_GYM);

				List<ClientNum> browselist = clientNumService.queryByList(browsemap);

				Integer browsenum = 0;

				for(ClientNum c : browselist){
					browsenum += c.getNum() == null ? 0 : c.getNum();
				}

				gym.setFavoritenum(favoritenum);
				gym.setSharenum(sharenum);
				gym.setBrowsenum(browsenum);

				List<ClientNum> cns = null;//统计量	

				if(!StringUtils.isEmpty(userid)){
					//统计量
					Map<String, Object> mstgMap = new HashMap<String, Object>();

					mstgMap.put("enabled", true);
					mstgMap.put("isDeleted", false);
					mstgMap.put("numtype",CommCode.SYSTEM_COUNT_FAVORITE);				
					mstgMap.put("clientid", Integer.valueOf(userid));
					mstgMap.put("acttype", CommCode.SYSTEM_CARESEL_RANGE_GYM);
					mstgMap.put("actid",gymid);

					cns = clientNumService.queryByList(mstgMap);//查询统计量
				}

				if(null==cns||cns.size()==0){
					gym.setIsFavorite(false);					
				}else{					
					gym.setIsFavorite(true);							
				}
				
				resPara.setGym(gym);

				//获取场馆特色课程
				Map<String, Object> mgMap = new HashMap<String,Object>();

				mgMap.put("enabled", true);
				mgMap.put("isDeleted", false);
				mgMap.put("gymid", gymid);
				mgMap.put("coursetype", unicourse);				
				mgMap.put("orderByClause", "gd.updatedate desc");

				//获取特色课程（无套餐,选择时间）
				List<GymnasiumDetail> gds = gymnasiumDetailService.queryList(mgMap, pageBounds);

				resPara.setCourses(gds);					

				//获取场馆其他课程
				Map<String, Object> elsemap = new HashMap<String,Object>();

				elsemap.put("enabled", true);
				elsemap.put("isDeleted", false);
				elsemap.put("gymid", gymid);	
				elsemap.put("coursetype", elsecourse);
				elsemap.put("orderByClause", "gd.updatedate desc");

				List<GymnasiumDetail> gdes = gymnasiumDetailService.queryList(elsemap, pageBounds);

				resPara.setElsecourse(gdes);				


				resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);

			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
			logger.info(jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public GymDetailReqPara getReqPara(HttpServletRequest request) {

		GymDetailReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new GymDetailReqPara());

			reqPara = (GymDetailReqPara) baseRequest.clone();

			String gymid = request.getParameter("gymid");			
			String clientid = request.getParameter("userid");
			String grade = request.getParameter("grade");
			String type = request.getParameter("type");
			String unicourse = request.getParameter("unicourse");
			String elsecourse = request.getParameter("elsecourse");

			logger.info("reqPara gymid: " + gymid);			
			logger.info("reqPara clientid: " + clientid);
			logger.info("reqPara grade: " + grade);
			logger.info("reqPara type: " + type);
			logger.info("reqPara unicourse: " + unicourse);
			logger.info("reqPara elsecourse: " + elsecourse);

			reqPara.setGymid(gymid);			
			reqPara.setUserid(clientid);
			reqPara.setGrade(grade);
			reqPara.setType(type);
			reqPara.setUnicourse(unicourse);
			reqPara.setElsecourse(elsecourse);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		GymDetailReqPara gdReqPara = (GymDetailReqPara) reqPara;
		GymDetailResPara gdResPara = (GymDetailResPara) resPara;

		if (StringUtils.isEmpty(gdReqPara.getGymid())) {
			gdResPara.setCode(CommCode.M_A000000);
			gdResPara.setMessage(CommCode.M_E000015);
		} else if(StringUtils.isEmpty(gdReqPara.getUnicourse())){
			gdResPara.setCode(CommCode.M_A000000);
			gdResPara.setMessage(CommCode.M_E000021);
		} else if(StringUtils.isEmpty(gdReqPara.getElsecourse())){
			gdResPara.setCode(CommCode.M_A000000);
			gdResPara.setMessage(CommCode.M_E000022);
		}else{
			flag = true;
		}
		return  flag;
	}
}
